"use strict";
cc._RF.push(module, '16804W4JrNI1KxlxZgikMk0', 'BulletManager');
// Script/Managers/BulletManager.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var AppEvent_1 = require("../Core/AppEvent");
var Bullet_1 = require("../Bullet");
var EventManager_1 = require("./EventManager");
var AppDef_1 = require("../Core/AppDef");
var WindowUtils_1 = require("../Tools/WindowUtils");
var BulletManager = /** @class */ (function (_super) {
    __extends(BulletManager, _super);
    function BulletManager() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.bulletPrefab = null;
        _this.bulletLayer = null;
        _this.bulletList = [[], []];
        _this.collectBulletPool = [new cc.NodePool(AppDef_1.WEAPON_TYPE.My + ''), new cc.NodePool(AppDef_1.WEAPON_TYPE.Enumy + '')];
        _this.maxNum = 10;
        return _this;
    }
    BulletManager.prototype.onLoad = function () {
        EventManager_1.addListener(AppEvent_1.EVT_CREATE_BULLET, this.createBullet, this);
        this.onScheduleMove();
        this.init();
    };
    BulletManager.prototype.init = function () {
    };
    BulletManager.prototype.onScheduleMove = function () {
        var _this = this;
        this.unschedule(function () {
            _this.onCheckOutsideWindow();
        });
        this.schedule(function () {
            _this.onCheckOutsideWindow();
        }, 0.02);
    };
    BulletManager.prototype.onCheckOutsideWindow = function () {
        for (var j = 0; j < 2; j++) {
            for (var i = 0; i < this.bulletList[j].length; i++) {
                var offset = 50;
                var pos = this.bulletList[j][i].position;
                if (pos.x > (WindowUtils_1.default.DEVICE_W + offset) || pos.x < -offset
                    || pos.y > (WindowUtils_1.default.DEVICE_H + offset) || pos.y < -offset) {
                    this.addCollectList(this.bulletList[j][i], i);
                }
            }
        }
    };
    BulletManager.prototype.addCollectList = function (node, index) {
        cc.log("_-- 回收池插入 addCollectList node.name  ", node.name);
        var script = node.getComponent(Bullet_1.default);
        script.onUnScheduleMove();
        if (this.collectBulletPool[script.bulletType].size() >= this.maxNum) {
            node.destroy();
        }
        else {
            this.collectBulletPool[script.bulletType].put(node);
            if (index) {
                this.bulletList[script.bulletType].splice(index, 1);
            }
            else {
                for (var j = 0; j < 2; j++) {
                    for (var i = 0; i < this.bulletList[j].length; i++) {
                        if (node.name == this.bulletList[j][i].name) {
                            this.bulletList[script.bulletType].splice(index, 1);
                        }
                    }
                }
            }
        }
    };
    BulletManager.prototype.checkCollectList = function (weaponType) {
        var length = this.collectBulletPool[weaponType].size();
        if (length > 0) {
            cc.log("_-- 回收池创建 createBullet weaponType ", weaponType);
            var bullet = this.collectBulletPool[weaponType].get();
            bullet.parent = this.bulletLayer;
            this.bulletList[weaponType].push(bullet);
            bullet.name = "bullet-weapon" + weaponType + '-index' + (this.bulletList[weaponType].length - 1);
            var script = bullet.getComponent(Bullet_1.default);
            script.onReset();
            return bullet;
        }
        else {
            cc.log("_-- 新建 createBullet weaponType ", weaponType);
            var bullet = cc.instantiate(this.bulletPrefab);
            this.bulletList[weaponType].push(bullet);
            bullet.name = "bullet-weapon" + weaponType + '-index' + (this.bulletList[weaponType].length - 1);
            this.bulletLayer.addChild(bullet);
            return bullet;
        }
    };
    BulletManager.prototype.createBullet = function (event) {
        var data = event.getUserData();
        // cc.log("_-- createBullet data ", data);
        var node = this.checkCollectList(data.weaponType);
        node.position = data.pos;
        var script = node.getComponent(Bullet_1.default);
        script.setP(data.weaponP);
        script.setBulletType(data.weaponType, data.from);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        var self = this;
        script.onBeginContact = function (contact, selfCollider, otherCollider) {
            cc.log('onBeginContact otherCollider ', otherCollider);
            var nameList = otherCollider.node.name.split("playerType=");
            cc.log('onBeginContact PLAYER_TYPE ', nameList[1]);
            if (nameList[1] == AppDef_1.PLAYER_TYPE.Enumy) {
                self.addCollectList(node, null);
                EventManager_1.dispatchEvent(AppEvent_1.EVT_PLAYER_HIT, { playerName: otherCollider.node.name });
            }
        };
    };
    BulletManager.prototype.Destroy = function () {
        this.collectBulletPool[0].clear();
        this.collectBulletPool[1].clear();
    };
    __decorate([
        property(cc.Prefab)
    ], BulletManager.prototype, "bulletPrefab", void 0);
    __decorate([
        property(cc.Node)
    ], BulletManager.prototype, "bulletLayer", void 0);
    BulletManager = __decorate([
        ccclass
    ], BulletManager);
    return BulletManager;
}(cc.Component));
exports.default = BulletManager;

cc._RF.pop();